package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.constant.BompAppParamConstant;
import cc.rengu.igas.bomp.common.dao.BankCodeInfoMapper;
import cc.rengu.igas.bomp.common.dao.impl.BankCodeInfoMapperImpl;
import cc.rengu.igas.bomp.common.entity.BankCodeInfo;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.bean.BankNameCodeInfoBean;
import cc.rengu.igas.bomp.facade.request.BankNameCodeQueryRequest;
import cc.rengu.igas.bomp.facade.response.BankNameCodeQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class BankNameCodeQueryService extends ManageService {

    @Override
    protected Object bizBeforeCust() throws Exception {
        BankNameCodeQueryRequest bankNameCodeQueryRequest = new BankNameCodeQueryRequest();
        ConvertUtil.convertOutput(bankNameCodeQueryRequest);
        return bankNameCodeQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        BankNameCodeQueryRequest requestParams = (BankNameCodeQueryRequest) request;
        BankNameCodeQueryResponse queryResponse = new BankNameCodeQueryResponse();
        BeanUtil.beanCopy(requestParams, queryResponse);
        BizResponse<BankNameCodeQueryResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setResult(queryResponse);

        BankCodeInfoMapper bankCodeInfoMapper = new BankCodeInfoMapperImpl();
        List<BankCodeInfo> bankCodeInfoList = new ArrayList<>();
        if (!StringUtil.isEmptyOrNull(requestParams.getCupsInstId())) {
            bankCodeInfoList = bankCodeInfoMapper.bompGetBankCodeInfoByCupsInstId(requestParams.getCupsInstId());
            if (CollectionUtils.isEmpty(bankCodeInfoList)) {
                rglog.info("根据查询条件查询到0条行名行号信息");
                queryResponse.setTotalPage(BompAppParamConstant.INT_ZERO);
                queryResponse.setTotalRows(BompAppParamConstant.INT_ZERO);
                queryResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                queryResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
                bizResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                bizResponse.setRspSysId(AppParamConstant.SYS_ID);
                return bizResponse;
            }
        } else {
            if (-1 == requestParams.getPageIndex() || -1 == requestParams.getPageSize()) {
                bankCodeInfoList = bankCodeInfoMapper.selectBankCodeInfoByCondition(requestParams.getBankName(), requestParams.getBankCode());
                if (CollectionUtils.isEmpty(bankCodeInfoList)) {
                    rglog.info("根据查询条件查询到0条行名行号信息");
                    queryResponse.setTotalPage(BompAppParamConstant.INT_ZERO);
                    queryResponse.setTotalRows(BompAppParamConstant.INT_ZERO);
                    queryResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                    queryResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
                    bizResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                    bizResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                    bizResponse.setRspSysId(AppParamConstant.SYS_ID);
                    return bizResponse;
                }
            } else {
                PageInfo pageInfo = new PageInfo();
                pageInfo.setPageIndex(requestParams.getPageIndex());
                pageInfo.setPageSize(requestParams.getPageSize());
                PageResult pageResult = bankCodeInfoMapper.selectBankCodeInfoByCondition(requestParams.getBankName(), requestParams.getBankCode(), pageInfo);
                if (null == pageResult || null == pageResult.getPageInfo() || null == pageResult.getResult() || pageResult.getResult().isEmpty()) {
                    rglog.info("根据查询条件查询到0条行名行号信息");
                    queryResponse.setTotalPage(BompAppParamConstant.INT_ZERO);
                    queryResponse.setTotalRows(BompAppParamConstant.INT_ZERO);
                    queryResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                    queryResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
                    bizResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                    bizResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                    bizResponse.setRspSysId(AppParamConstant.SYS_ID);
                    return bizResponse;
                }

                /* 根据取出记录条数做对应处理 */
                for (Object object : pageResult.getResult()) {
                    bankCodeInfoList.add((BankCodeInfo) object);
                }
                queryResponse.setTotalPage(pageResult.getPageInfo().getTotalPage());
                queryResponse.setTotalRows(pageResult.getPageInfo().getTotalRows());
            }
        }

        if (!CollectionUtils.isEmpty(bankCodeInfoList)) {
            List<BankNameCodeInfoBean> bankNameCodeInfoBeans = new ArrayList<>();
            bankCodeInfoList.forEach(item -> convBankNameCode(item, bankNameCodeInfoBeans));
            List<BankNameCodeInfoBean> bankNameCodeInfoBeanList = bankNameCodeInfoBeans;
            //行号过滤，只展示一条
            bankNameCodeInfoBeanList = bankNameCodeInfoBeanList.stream().filter(distinctByBankCode(BankNameCodeInfoBean::getBankBranchCode)).collect(Collectors.toList());
            queryResponse.setBankNameCodeInfoBeanList(bankNameCodeInfoBeanList);
        } else {
            rglog.error("根据行名行号 无法查询到人行信息");
        }

        rglog.info("SIZE=<{}>", bankCodeInfoList.size());
        /* 按照接口转换基础产品信息并响应给请求方 */
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        BankNameCodeQueryResponse bankNameCodeQueryResponse = (BankNameCodeQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(bankNameCodeQueryResponse);
    }

    /**
     * 将数据库表结构对象转换成接口对象
     *
     * @param bankCodeInfo          数据库表结构对象
     * @param bankNameCodeInfoBeans 接口对象
     */
    private void convBankNameCode(BankCodeInfo bankCodeInfo, List<BankNameCodeInfoBean> bankNameCodeInfoBeans) {
        if ("01".equals(bankCodeInfo.getBankBranchStatus())) {
            BankNameCodeInfoBean bankNameCodeInfoBean = new BankNameCodeInfoBean();
            BeanUtil.beanCopy(bankCodeInfo, bankNameCodeInfoBean);
            bankNameCodeInfoBean.setBankBranchCode(bankCodeInfo.getBankBranchCode());
            bankNameCodeInfoBean.setBankBranchName(bankCodeInfo.getBankBranchName());
            bankNameCodeInfoBean.setBankName(bankCodeInfo.getBankName());
            bankNameCodeInfoBeans.add(bankNameCodeInfoBean);
        }
    }

    private static <T> Predicate<T> distinctByBankCode(Function<? super T, Object> keyExtractor) {
        Set<Object> set = ConcurrentHashMap.newKeySet();
        return t -> set.add(keyExtractor.apply(t));
    }
}
